package cn.bugstack.mybatis.binding;

import java.lang.reflect.Method;

import cn.bugstack.mybatis.mapping.MappedStatement;
import cn.bugstack.mybatis.mapping.SqlCommandType;
import cn.bugstack.mybatis.session.Configuration;
import cn.bugstack.mybatis.session.SqlSession;

/**
 * description: mapper方法
 * taskId：
 * <pre>
 * change history:
 * date             defect#             person                comments
 * ---------------------------------------------------------------------
 * 2024/11/26       ********         zhaochaoyue        create file
 * </pre>
 *
 * @author: zhaochaoyue
 * @date: 2024/11/26 10:09
 * @copyright: 2024
 */
public class MapperMethod {
    
    private final SqlCommand command;
    
    public MapperMethod(Class<?> mapperInterface,Method method,Configuration configuration){
        this.command = new SqlCommand(configuration, mapperInterface, method);
    }
    
    public Object execute(SqlSession sqlSession,Object[] args){
        Object result =null;
        switch (command.getType()) {
            case INSERT:
                break;
            case DELETE:
                break;
            case UPDATE:
                break;
            case SELECT:
                result = sqlSession.selectOne(command.getName(),args);
                break;
            default:
                throw new RuntimeException("Unknown execution method for: " + command.getName());
        }
        return  result;
    }
    
    
    public class SqlCommand{
        private final String name;
        private final SqlCommandType type;

        public SqlCommand(Configuration configuration,Class<?> mapperInterface, Method method) {
            //全限定类名
            String statementName = mapperInterface.getName() + "." + method.getName();
            MappedStatement ms = configuration.getMappedStatement(statementName);
            this.name = ms.getId();
            this.type = ms.getCommandType();
        }

        public String getName() {
            return name;
        }

        public SqlCommandType getType() {
            return type;
        }
    }
}
